package copyengine.ui.component.list
{
	import copyengine.ui.component.list.impl.CEMListLayoutQueueData;


	public final class CEMListCore
	{
		public static const LAYOUT_HORIZONTAL:int=1; //水平
		public static const LAYOUT_VERTICAL:int=2; //垂直

		//所有的CellRender
		private var allCellRenderVector:Vector.<ICEMListCellRender>;

		//ordered和dirty两个Vector,用于CellRender的排列快速计算
		private var orderedCellRenderVector:Vector.<ICEMListCellRender>;
		private var dirtyCellRenderVector:Vector.<ICEMListCellRender>;

		private var dataProvider:Array;
		private var calculateHelper:ICEMListCalculateHelper;

		private var _scrollPosition:int;

		public function CEMListCore()
		{
			super();
		}

		//============================================//
		//==Initialize&Dispose  Function
		//============================================//
		public function initialize(_allCellRenderVector:Vector.<ICEMListCellRender>,
								   _calculateHelper:ICEMListCalculateHelper,
								   _dataProvider:Array):void
		{
			dataProvider=_dataProvider;
			calculateHelper=_calculateHelper;
			calculateHelper.setDataProvier(_dataProvider);

			allCellRenderVector=_allCellRenderVector;
			dirtyCellRenderVector=new Vector.<ICEMListCellRender>();
			orderedCellRenderVector=new Vector.<ICEMListCellRender>();
			for each (var cellRender:ICEMListCellRender in allCellRenderVector)
			{
				//先将CellRender的Index设置为-1,这样第一次不会在fastFind中找到任何的CellRender
				cellRender.cellRenderIndex=-1;
				orderedCellRenderVector.push(cellRender);
			}
			//将ScrollPosition设置为0,然后刷新List
			//因为初始化时候刚传入DataProvier,外部无法计算出最大ScrollPosition等先关信息
			//所以无法在初始化时候由外部传入一个ScrollPosition,及均以0刷新
			_scrollPosition=0;
			layoutAndDrawAllCellRender();
		}

		public function dispose():void
		{
		}

		//============================================//
		//==Public Interface  Function
		//============================================//
		public function refresh(_newDataProvider:Array=null, _newScrollPosition:int=int.MIN_VALUE):void
		{
			if (_newDataProvider != null)
			{
				dataProvider=_newDataProvider;
				calculateHelper.setDataProvier(dataProvider)
			}

			if (_newScrollPosition != int.MIN_VALUE)
			{
				scrollPosition=_newScrollPosition;
			}

			for each (var cellRender:ICEMListCellRender in allCellRenderVector)
			{
				cellRender.refresh();
			}
		}

		public function get scrollPosition():int  { return _scrollPosition; }

		public function set scrollPosition(_position:int):void
		{
			if (_position != _scrollPosition)
			{
				_scrollPosition=_position;
				layoutAndDrawAllCellRender();
			}
		}

		public function get maxScrollPosition():int  { return calculateHelper.getMaxScrollPosition(); }

		//========================//
		//==Private  Function
		//=========================//

		private function layoutAndDrawAllCellRender():void
		{
			//交换dirty和ordered两个Vector,用于后续的计算
			var tempCellRenderVector:Vector.<ICEMListCellRender>=dirtyCellRenderVector;
			dirtyCellRenderVector=orderedCellRenderVector;
			orderedCellRenderVector=tempCellRenderVector;

			//计算出需要渲染的序列
			var layoutQueueVector:Vector.<CEMListLayoutQueueData>=calculateHelper.getCalculatedLayoutQueue(_scrollPosition);

			//先尝试快速的设置CellRender再设置剩余CellRender
			fastResetCellRenderByLayoutQueue(layoutQueueVector);
			recycleAndResetCellRenderByQueue(layoutQueueVector);
			setAllCellRenderActivePropertyAndCleanDirtyVector();
		}


		private function fastResetCellRenderByLayoutQueue(_layoutQueueVector:Vector.<CEMListLayoutQueueData>):void
		{
			for (var i:int=0; i < _layoutQueueVector.length; i++)
			{
				var currentLayoutQueueData:CEMListLayoutQueueData=_layoutQueueVector[i];
				if (currentLayoutQueueData.isEnable && !currentLayoutQueueData.isLayouted)
				{
					//尝试查找当前位置是否含有CellRender
					var cellRender:ICEMListCellRender=tryToGetAndRemoveCellRenderFromDirtyByIndex(currentLayoutQueueData.cellRenderIndex);
					if (cellRender)
					{
						//仅改变位置
						cellRender.setLayoutPosition(currentLayoutQueueData.layoutPosX, currentLayoutQueueData.layoutPosY);

						//当前CellRender已经排好位置
						currentLayoutQueueData.isLayouted=true;
						orderedCellRenderVector.push(cellRender);
					}
				}
				else
				{
					break;
				}
			}
		}

		private function recycleAndResetCellRenderByQueue(_layoutQueueVector:Vector.<CEMListLayoutQueueData>):void
		{
			for (var i:int=0; i < _layoutQueueVector.length; i++)
			{
				var currentLayoutQueueData:CEMListLayoutQueueData=_layoutQueueVector[i];
				if (currentLayoutQueueData.isEnable && !currentLayoutQueueData.isLayouted)
				{
					var cellRender:ICEMListCellRender=dirtyCellRenderVector.pop();

					//回收并渲染
					cellRender.recycle();
					cellRender.draw(dataProvider[currentLayoutQueueData.cellRenderIndex]);
					cellRender.setLayoutPosition(currentLayoutQueueData.layoutPosX, currentLayoutQueueData.layoutPosY);

					//当前CellRender已经排好位置
					currentLayoutQueueData.isLayouted=true;
					orderedCellRenderVector.push(cellRender);
				}
			}
		}

		private function tryToGetAndRemoveCellRenderFromDirtyByIndex(_cellRenderIndex:int):ICEMListCellRender
		{
			for each (var cellRender:ICEMListCellRender in dirtyCellRenderVector)
			{
				if (cellRender.cellRenderIndex == _cellRenderIndex)
				{
					return cellRender;
				}
			}
			return null;
		}

		/**
		 *将所有已经排好序的CellRender设置为激活(及在屏幕上显示),没有排序的(及根据当前ScrollPosition计算出来不需要再屏幕显示)
		 * CelllRender设置为非激活,并将这些设置了非激活状态的CellRender从DirtyVector中pop出来再Push到OrderVector中去
		 */
		private function setAllCellRenderActivePropertyAndCleanDirtyVector():void
		{
			var cellRender:ICEMListCellRender;
			for each (cellRender in orderedCellRenderVector)
			{
				cellRender.setIsActive(true);
			}
			while (dirtyCellRenderVector.length > 0)
			{
				cellRender=dirtyCellRenderVector.pop();
				cellRender.setIsActive(false);
				orderedCellRenderVector.push(cellRender);
			}
		}

	}
}
